from typing import List


class Solution:

    def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])

        #定义一个字典结构key是矩阵对应的值，value是矩阵值对应的下标
        pos = [0] * (m * n + 1)

        #定义两个个一维数组
        row_cnt, col_cnt =[0] * m, [0] * n

        #遍历双重list
        for i, row in enumerate(mat):
            for j, x in enumerate(row): pos[x] = (i, j)

        for i, num in enumerate(arr):
            r, c = pos[num]
            row_cnt[r] += 1
            col_cnt[c] += 1
            if row_cnt[r] == n or col_cnt[c] == m: return i



if __name__ == '__main__':
    # 测试示例
    arr1 = [1, 3, 4, 2]
    mat1 = [[1, 4], [2, 3]]
    print(Solution().firstCompleteIndex(arr1, mat1))  # 输出：2

    arr2 = [2, 8, 7, 4, 1, 3, 5, 6, 9]
    mat2 = [[3, 2, 5], [1, 4, 6], [8, 7, 9]]
    print(Solution().firstCompleteIndex(arr2, mat2))  # 输出：3
